all_tests = [
  '232c93d07b74.c',
  '35fa71a030ca.c',
  '500f9fbadef8.c',
  '7ad0e4b2f83c.c',
  '8a9973408177.c',
  '917257daa0fe.c',
  'a0908ae19763.c',
  'a4c0b3decb33.c',
  'accept-link.c',
  'accept-non-empty.c',
  'accept-reuse.c',
  'accept-test.c',
  'accept.c',
  'across-fork.c',
  'b19062a56726.c',
  'b5837bd5311d.c',
  'bind-listen.c',
  'buf-ring-nommap.c',
  'buf-ring-put.c',
  'buf-ring.c',
  'ce593a6c480a.c',
  'close-opath.c',
  'cmd-discard.c',
  'conn-unreach.c',
  'connect-rep.c',
  'connect.c',
  'coredump.c',
  'cq-full.c',
  'cq-overflow.c',
  'cq-peek-batch.c',
  'cq-ready.c',
  'cq-size.c',
  'd4ae271dfaae.c',
  'd77a67ed5f27.c',
  'defer-taskrun.c',
  'defer-tw-timeout.c',
  'defer.c',
  'double-poll-crash.c',
  'drop-submit.c',
  'eeed8b54e0df.c',
  'empty-eownerdead.c',
  'eploop.c',
  'epwait.c',
  'eventfd-disable.c',
  'eventfd-reg.c',
  'eventfd-ring.c',
  'eventfd.c',
  'evfd-short-read.c',
  'evloop.c',
  'exec-target.c',
  'exit-no-cleanup.c',
  'fadvise.c',
  'fallocate.c',
  'fc2a85cb02ef.c',
  'fd-install.c',
  'fd-pass.c',
  'fdinfo-sqpoll.c',
  'fdinfo.c',
  'fifo-nonblock-read.c',
  'file-exit-unreg.c',
  'file-register.c',
  'file-update.c',
  'file-verify.c',
  'files-exit-hang-poll.c',
  'files-exit-hang-timeout.c',
  'fixed-buf-iter.c',
  'fixed-buf-merge.c',
  'fixed-link.c',
  'fixed-reuse.c',
  'fixed-seg.c',
  'fpos.c',
  'fsnotify.c',
  'fsync.c',
  'hardlink.c',
  'ignore-single-mmap.c',
  'init-mem.c',
  'io-cancel.c',
  'io_uring_enter.c',
  'io_uring_passthrough.c',
  'io_uring_setup.c',
  'iopoll-leak.c',
  'iopoll-overflow.c',
  'iopoll.c',
  'iowait.c',
  'kallsyms.c',
  'lfs-openat-write.c',
  'lfs-openat.c',
  'link-timeout.c',
  'link.c',
  'link_drain.c',
  'linked-defer-close.c',
  'madvise.c',
  'min-timeout-wait.c',
  'min-timeout.c',
  'mkdir.c',
  'msg-ring-fd.c',
  'msg-ring-flags.c',
  'msg-ring-overflow.c',
  'msg-ring.c',
  'multicqes_drain.c',
  'napi-test.c',
  'no-mmap-inval.c',
  'nolibc.c',
  'nop-all-sizes.c',
  'nop.c',
  'ooo-file-unreg.c',
  'open-close.c',
  'open-direct-link.c',
  'open-direct-pick.c',
  'openat2.c',
  'personality.c',
  'pipe-bug.c',
  'pipe-eof.c',
  'pipe-reuse.c',
  'pipe.c',
  'poll-cancel-all.c',
  'poll-cancel-ton.c',
  'poll-cancel.c',
  'poll-link.c',
  'poll-many.c',
  'poll-mshot-overflow.c',
  'poll-mshot-update.c',
  'poll-race-mshot.c',
  'poll-race.c',
  'poll-ring.c',
  'poll-v-poll.c',
  'poll.c',
  'pollfree.c',
  'probe.c',
  'read-before-exit.c',
  'read-inc-file.c',
  'read-mshot-empty.c',
  'read-mshot-stdin.c',
  'read-mshot.c',
  'read-write.c',
  'recv-bundle-short-ooo.c',
  'recv-inc-ooo.c',
  'recv-msgall.c',
  'recv-mshot-fair.c',
  'recv-multishot.c',
  'recvsend_bundle-inc.c',
  'recvsend_bundle.c',
  'reg-fd-only.c',
  'reg-hint.c',
  'reg-reg-ring.c',
  'reg-wait.c',
  'regbuf-clone.c',
  'regbuf-merge.c',
  'register-restrictions.c',
  'rename.c',
  'resize-rings.c',
  'ring-leak.c',
  'ring-leak2.c',
  'ringbuf-read.c',
  'ringbuf-status.c',
  'rsrc_tags.c',
  'rw_merge_test.c',
  'self.c',
  'send_recv.c',
  'sendmsg_iov_clean.c',
  'shared-wq.c',
  'short-read.c',
  'shutdown.c',
  'sigfd-deadlock.c',
  'single-issuer.c',
  'skip-cqe.c',
  'socket-getsetsock-cmd.c',
  'socket-io-cmd.c',
  'socket-nb.c',
  'socket-rw-eagain.c',
  'socket-rw-offset.c',
  'socket-rw.c',
  'socket.c',
  'splice.c',
  'sq-full.c',
  'sq-poll-dup.c',
  'sq-poll-kthread.c',
  'sq-poll-share.c',
  'sq-space_left.c',
  'sqpoll-disable-exit.c',
  'sqpoll-exec.c',
  'sqpoll-exit-hang.c',
  'sqpoll-sleep.c',
  'sqwait.c',
  'stdout.c',
  'submit-and-wait.c',
  'submit-link-fail.c',
  'submit-reuse.c',
  'symlink.c',
  'sync-cancel.c',
  'teardowns.c',
  'thread-exit.c',
  'timeout-new.c',
  'timeout.c',
  'timerfd-short-read.c',
  'timestamp.c',
  'truncate.c',
  'tty-write-dpoll.c',
  'unlink.c',
  'uring_cmd_ublk.c',
  'vec-regbuf.c',
  'version.c',
  'wait-timeout.c',
  'waitid.c',
  'wakeup-hang.c',
  'wq-aff.c',
  'xattr.c',
  'xfail_prep_link_timeout_out_of_scope.c',
  'zcrx.c',
]

fs = import('fs')

has_hugepages = fs.read('/proc/sys/vm/nr_hugepages').to_int() > 0

if has_hugepages
  all_tests += ['fixed-hugepage.c', 'io_uring_register.c']
endif

if has_statx or glibc_statx
  all_tests += ['statx.c']
endif

if has_cxx
  all_tests += ['sq-full-cpp.cc']
endif

if has_futexv
  all_tests += ['futex-kill.c', 'futex.c']
endif

runtests_sh = find_program('runtests.sh')

xcflags = ['-Wno-sign-compare']
if has_stringop_overflow
  xcflags = xcflags + ['-Wstringop-overflow=0']
endif
if has_array_bounds
  xcflags = xcflags + [
    cc.has_argument('-Warray-bounds=0') ? '-Warray-bounds=0' : '-Warray-bounds',
  ]
endif

has_io_uring_kernel_support = host_machine.cpu() in ['x86', 'x86_64', 'aarch64']

fs = import('fs')

foreach test_source : all_tests
  test_name = fs.stem(test_source)
  executable(
    test_name,
    [test_source, 'helpers.c'],
    c_args: xcflags,
    cpp_args: xcflags,
    include_directories: inc,
    link_with: liburing.get_static_lib(),
    dependencies: [thread_dep],
    install: true,
    install_dir: get_option('datadir') / 'liburing-test',
  )

  # Not all architectures support io_uring enough to run the tests.
  # The tests will still be built to ensure that liburing was configured correctly.
  if has_io_uring_kernel_support
    test(
      test_name,
      runtests_sh,
      args: test_name,
      should_fail: test_source.startswith('xfail_'),
      workdir: meson.current_build_dir(),
      timeout: 300,
    )
  endif
endforeach

add_test_setup(
  'runtests',
  exclude_suites: ['loop', 'parallel'],
  is_default: true,
)

configure_file(
  input: 'runtests.sh',
  output: 'runtests.sh',
  copy: true,
)

configure_file(
  input: 'runtests-loop.sh',
  output: 'runtests-loop.sh',
  copy: true,
)

configure_file(
  input: 'config',
  output: 'config.local',
  copy: true,
)

install_data(
  'runtests.sh',
  'runtests-loop.sh',
  install_dir: get_option('datadir') / 'liburing-test',
)
